<?php

class WeeklyCalendar{
	const MONDAY=0;
	const TUESDAY=1;
	const WEDNESDAY=2;
	const THURSDAY=3;
	const FRIDAY=4;
	const SATURDAY=5;
	const SUNDAY=6;
	
	private $arrDays=array(self::MONDAY=>'Lunes',
	                       self::TUESDAY=>'Martes',
	                       self::WEDNESDAY=>'Miercoles',
	                       self::THURSDAY=>'Jueves',
	                       self::FRIDAY=>'Viernes',
	                       self::SATURDAY=>'Sabado',
	                       self::SUNDAY=>'Domingo');
	
	private $Calendar=array();
	public function newAnnotation($day_number, $hour_number, $duration=1 ){
		$enabled=$this->is_enabled($day_number, $hour_number, $duration);
		if($enabled===true){
			$this->updateAnnotation($day_number, $hour_number, $duration);
		}	else{
			throw new HourBusyException($this, sprintf("Hora Ocupada por '%s'",$enabled));
		}
	}
	
	private function removeAnnotation($day_number, $hour_number){
		$numberParent=$this->Calendar[$day_number][$hour_number];
		$i=0;
		do{
		  if(isset($this->Calendar[$day_number][$hour_number+$i]) && $this->Calendar[$day_number][$hour_number+$i]==$hour_number ){
		  	unset($this->Calendar[$day_number][$hour_number+$i]);
		  }	else{
		  	break;
		  }
		}while($i++);
	}
  
	private function updateAnnotation($day_number, $hour_number, $duration){
	  for($i=0;$i<$duration;$i++){
      $this->Calendar[$day_number][$hour_number+$i]=$hour_number;
    }
	}
		
	public function is_enabled($day_number, $hour_number, $duration){
		for($i=0;$i<$duration;$i++)
		if(isset($this->Calendar[$day_number][$hour_number+i])){
			return $this->Calendar[$day_number][$hour_number+i];
		}
		
		return true;
	}
	
	
}
?>